
local w0,h0 = 322,244
local ui = class.panel:builder 
{
    x = 0,
    y = 200,
    w = w0,
    h = h0,
    normal_image = 'ui\\底板.tga',
    show_flag = false,
}
ui:set_level(99)
ui:add_text('每秒伤害',0,5,w0,25,12,5)
local arts = {
   [[ui\右-c.tga]], 
   [[ui\右-l.tga]], 
   [[ui\左-c.tga]], 
   [[ui\左-l.tga]], 
}
local button = class.button.create('',10,ui.y,14,25)
button.highlight = button:add_panel('')
function button:on_button_clicked()
    if ui:is_show() then
        ui:hide()
        self:set_normal_image(arts[1])
        self.highlight:set_normal_image(arts[2])
    else
        ui:show()
        self:set_normal_image(arts[3])
        self.highlight:set_normal_image(arts[4])
    end
end
button:on_button_clicked()

local panels = {}
local size = 5
local w = 291
local h = 35
local y = 35
local art = {
    [[ReplaceableTextures\TeamColor\TeamColor00.blp]],
    [[ReplaceableTextures\TeamColor\TeamColor01.blp]],
    [[ReplaceableTextures\TeamColor\TeamColor02.blp]],
    [[ReplaceableTextures\TeamColor\TeamColor03.blp]],
}

ac.each_player(function(player)
    local panel = ui:add_panel('',h+5,y,w-h+10,h)
    panel.icon = panel:add_panel([[ReplaceableTextures\CommandButtons\BTNSkeletonArcher.blp]],-h,0,h,h)
    panel.load1 = panel:add_panel([[ui\物理2.tga]],5,5,panel.w*0.5,panel.h-10)
    panel.load2 = panel:add_panel([[ui\魔法.tga]],5,5,panel.w*0.5,panel.h-10)
    panel.name = panel:add_text(player:get_name(),10,0,panel.w,h,12,4)
    panel.dps = panel:add_text('999999',0,0,panel.w-5,nil,12,6)
    panel.load_w = panel.w - 10
    panel.player = player
    y = y + h +5
    panels[#panels + 1] = panel
end)

ui:set_height(y)

local key1 = '物理伤害统计'
local key2 = '魔法伤害统计'
ui.list = {}

function ui:up_date(unit)
    local max = 0
    local time = math.min(ac.clock()/1000,60)
    table.sort(self.list,function(a,b)
        return (a['总伤害统计'] or 0)>(b['总伤害统计'] or 0)
    end)
    for a,info in ipairs(self.list) do
        local sum = info['总伤害统计'] or 0
        max = math.max(max,sum)
    end
    for a,panel in ipairs(panels) do
        local info = self.list[a]
        if info then
            local player = info.player
            local hero = player.hero
            local dps2 = info[key1] or 0
            local dps3 = info[key2] or 0
            local dps1 = dps2 + dps3
            local rate1 = 0
            local rate2 = 0
            local rate3 = max>0 and math.min(dps1/max,1) or 0
            if dps1>0 then
                rate1 = dps2/dps1 * rate3
                rate2 = dps3/dps1 * rate3
            end
            panel.name:set_text(player:get_name())
            panel.load1:set_width(rate1*panel.load_w+0.01)
            panel.load2:set_width(rate2*panel.load_w+0.01)
            panel.load1:set_position(rate2*panel.load_w+5,panel.load1.y)

            panel.dps:set_text(format_number1(dps1))
            if hero then
                panel.icon:set_normal_image(hero:get_slk("Art", ""))
            end
            panel:show()
        else
            panel:hide()
        end
    end
end

ac.loop(30,function()
    ui:up_date(unit)
end)

ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
    local list = {
        player = player,
        ['物理伤害统计'] = 0,
        ['魔法伤害统计'] = 0,
        ['总伤害统计'] = 0,
    }
    table.insert(ui.list,list)
    hero:event '单位-造成伤害结束'(function(_,_,_,data)
        local damage = data.real_damage/60
        local key = '物理伤害统计'
        if data.damage_type=='魔法' then
            key = '魔法伤害统计'
        end
        list[key] = list[key] + damage
        list['总伤害统计'] = list['总伤害统计'] + damage
        ac.wait(60*1000,function()
            list[key] = list[key] - damage
            list['总伤害统计'] = list['总伤害统计'] - damage
        end)
    end)
end)